package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysMenuFavorite;
import com.ruoyi.system.service.ISysMenuFavoriteService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收藏菜单Controller
 * 
 * @author kuaidou
 * @date 2021-09-01
 */
@Controller
@RequestMapping("/system/favorite")
public class SysMenuFavoriteController extends BaseController
{
    private String prefix = "system/favorite";

    @Autowired
    private ISysMenuFavoriteService sysMenuFavoriteService;

    @RequiresPermissions("system:favorite:view")
    @GetMapping()
    public String favorite()
    {
        return prefix + "/favorite";
    }

    /**
     * 查询用户收藏菜单列表
     */
    @RequiresPermissions("system:favorite:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMenuFavorite sysMenuFavorite)
    {
        startPage();
        List<SysMenuFavorite> list = sysMenuFavoriteService.selectSysMenuFavoriteList(sysMenuFavorite);
        return getDataTable(list);
    }

    /**
     * 导出用户收藏菜单列表
     */
    @RequiresPermissions("system:favorite:export")
    @Log(title = "用户收藏菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMenuFavorite sysMenuFavorite)
    {
        List<SysMenuFavorite> list = sysMenuFavoriteService.selectSysMenuFavoriteList(sysMenuFavorite);
        ExcelUtil<SysMenuFavorite> util = new ExcelUtil<SysMenuFavorite>(SysMenuFavorite.class);
        return util.exportExcel(list, "用户收藏菜单数据");
    }

    /**
     * 新增用户收藏菜单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户收藏菜单
     */
    @RequiresPermissions("system:favorite:add")
    @Log(title = "用户收藏菜单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMenuFavorite sysMenuFavorite)
    {
        return toAjax(sysMenuFavoriteService.insertSysMenuFavorite(sysMenuFavorite));
    }

    /**
     * 修改用户收藏菜单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysMenuFavorite sysMenuFavorite = sysMenuFavoriteService.selectSysMenuFavoriteById(id);
        mmap.put("sysMenuFavorite", sysMenuFavorite);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户收藏菜单
     */
    @RequiresPermissions("system:favorite:edit")
    @Log(title = "用户收藏菜单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMenuFavorite sysMenuFavorite)
    {
        return toAjax(sysMenuFavoriteService.updateSysMenuFavorite(sysMenuFavorite));
    }

    /**
     * 删除用户收藏菜单
     */
    @RequiresPermissions("system:favorite:remove")
    @Log(title = "用户收藏菜单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMenuFavoriteService.deleteSysMenuFavoriteByIds(ids));
    }

    @PostMapping( "/sccd")
    @ResponseBody
    public AjaxResult sccd(SysMenuFavorite sysMenuFavorite) {
        return AjaxResult.success(sysMenuFavoriteService.insertSysMenuFavorite(sysMenuFavorite));
    }

}
